let fs = require('fs');
const path = require('path');
let Promise = require('./promise');

/*function read(url){ // angular Q库
    return new Promise((resolve,reject)=>{
        fs.readFile(path.resolve(__dirname,url),'utf8',function(err,data){
            if(err){
                reject(err);
            }else{
                resolve(data);
            }
        });
    })
}*/
//promise是用来嵌套的但上面这样还是套了一层，so为了减少一层这样的套用可以使用Promise.defer()

Promise.defer = Promise.deferred = function() {
    let dfd = {}; // ←会围绕这个对象来测试
    dfd.promise = new Promise((resolve, reject) => {
        dfd.resolve = resolve;
        dfd.reject = reject;
    });
    return dfd;
};

function read(){ // angular Q库
    let defer = Promise.defer();
    fs.readFile('./note.md','utf8',function(err,data){
        if(err){
            defer.reject(err);
        }else{
            defer.resolve(data);
        }
    });
    return defer.promise
}
read().then(data=>{
    console.log(data);
});
